package com.itzhoun.sort;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;

/**
 * @author zhounan
 * @Title: design-patterns
 * @Description: TODO
 * @date 30/7/2023下午 9:33
 */
public class AgeSortStrategy implements SortStrategy, Comparator<Person> {

    private String type;

    @Override
    public void sort(List<Person> personList, String type) {
        this.type = type;
        personList.sort(this);
    }

    @Override
    public int compare(Person o1, Person o2) {
        switch (type) {
            case "DESC":
                if (Objects.equals(o1.getAge(), o2.getAge())) {
                    return o2.getId() - o1.getId();
                }
                return o2.getAge() - o1.getAge();
            case "ASC":
                if (Objects.equals(o1.getAge(), o2.getAge())) {
                    return o1.getId() - o2.getId();
                }
                return o1.getAge() - o2.getAge();
            default:
                return 0;
        }

    }


}
